package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;





import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import model.AppModel;
import model.ModelException;
import model.Template;


import view.TemplateView;

import view.ViewHelpers;

/**
 * Controls the Templates view
 * 
 * This controller has a reference to the main model, and the current view it is working on.
 * It assigns handlers to events for the edit, add, delete buttons and specifies the actions
 * to invoke upon triggering these events. 
 * 
 * @author MUSAB
 *
 * @version $Revision: 1.0 $
 */
public class TemplateViewController {
	
	// Application model, view and main controller
	private AppModel model;
	
	// The view to work on
	private TemplateView view;
	

	/**
	 * Constructor takes the app model
	 * @param model
	 */
	public TemplateViewController(AppModel model) {
		
		this.model = model; 
		view = new TemplateView();
		
		refreshView();
		assignHandlersToEvents();
		view.showDialog();
	}
	
	/**
	 * Load templates into view
	 */
	private void refreshView(){
		
		view.getTemplates().removeAllElements();
		for (Template template : model.getUser().getTemplates()){
			view.getTemplates().addElement(template.getTitle());
		}
	}

	
	/**
	 * Adds handlers to events for the template view
	 */
	private void assignHandlersToEvents() {
		
		/**
		 * Selection changed for jlist
		 */
		view.getTemplatesList().addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent e) {
				actionShowTemplatePreview();
			}
		});
	
		
		
		/**
		 * Add new template button click
		 */
		view.getAddNewBtn().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				actionCreateNewTemplate();
			}
		});
		
		/**
		 * Edit template button click
		 */
		view.getEditbtn().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				actionEditTemplate();
			}
		});
		
		/**
		 * delete template button click
		 */
		view.getDeleteBtn().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				actionDeleteTemplate();
			}
		});
		
	}

	/**
	 * Shows the template preview of the template selected in the list
	 */
	protected void actionShowTemplatePreview() {
		
		String tempalteTitle = (String) view.getTemplatesList().getSelectedValue();
		
		// If none selected, clear its
		if (tempalteTitle == null){
			view.getBody().setText("");
			return;
		}
		
		// Show selected template
		String body = model.getUser().getTemplate(tempalteTitle).getBody();
		view.getBody().setText(body);
	}

	/**
	 * Confirms for template deletion and then deletes that template
	 */
	protected void actionDeleteTemplate() {
		
		String tempalteTitle = (String) view.getTemplatesList().getSelectedValue();
		if (tempalteTitle == null){
			ViewHelpers.alertError("Please select a template first");
			return;
		}
		
		if (ViewHelpers.confirm("Are you sure you want to delete this template?")){
			try {
				int id = model.getUser().getTemplate(tempalteTitle).getId();
				model.getUser().deleteTemplate(id);
				refreshView();
			} catch (ModelException e) {
				ViewHelpers.alertError("Could not delete the template, please try again later");
				e.printStackTrace();
			}
		}
	}

	/**
	 * Shows the popup to edit an existing template
	 */
	protected void actionEditTemplate() {
		
		String tempalteTitle = (String) view.getTemplatesList().getSelectedValue();
		if (tempalteTitle == null){
			ViewHelpers.alertError("Please select a template first");
			return;
		}
		
		Template t = null;
		for (Template template : model.getUser().getTemplates()){
			if (template.getTitle().trim().equalsIgnoreCase(tempalteTitle)){
				t = template;
				break;
			}
		}
		new EditTemplateController(model, t);
		refreshView();
	}

	/**
	 * Shows the popup to add new template
	 */
	protected void actionCreateNewTemplate() {
		new EditTemplateController(model, null);
		refreshView();
	}

	
	
}
